package com.codeshellme.bookmanager.controller;

import com.codeshellme.bookmanager.dto.UmsMenu;
import com.codeshellme.bookmanager.service.UmsMenuService;
import com.codeshellme.bookmanager.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class MenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    @RequestMapping("/menu")
    public String menu() {
        return "menu";
    }

    // 查询所有菜单
    @ResponseBody
    @RequestMapping("/queryMenus")
    public CommonResult queryMenus() {
        List<Map<String, Object>> menus = umsMenuService.queryMenus();
        return CommonResult.success(menus);
    }

    // 根据 id 查询菜单
    @ResponseBody
    @RequestMapping("/queryMenuById")
    public CommonResult queryMenuById(@RequestParam(value = "id") Integer id) {
        Map<String, Object> menu = umsMenuService.queryMenuById(id);
        if (menu != null) {
            return CommonResult.success(menu);
        } else {
            return CommonResult.failed("没有相应的菜单");
        }
    }

    // 查询所有的菜单目录
    @ResponseBody
    @RequestMapping("/queryAllDirMenus")
    public CommonResult queryAllDirMenus() {
        List<Map<String, Object>> menus = umsMenuService.queryAllDirMenus();
        return CommonResult.success(menus);
    }

    // 据用户ID，查询它所属的角色，并查询该角色的菜单
    @ResponseBody
    @RequestMapping("/queryRoleMenusByUserId")
    public CommonResult queryRoleMenusByUserId(@RequestParam(value = "id") Integer userId) {
        List<Map<String, Object>> menus = umsMenuService.queryRoleMenusByUserId(userId);
        return CommonResult.success(menus);
    }

    // 据用户名，查询它的 id，再查询它所属的角色，并查询该角色的菜单
    @ResponseBody
    @RequestMapping("/queryRoleMenusByUserName")
    public CommonResult queryRoleMenusByUserName(@RequestParam(value = "name") String userName) {
        List<Map<String, Object>> menus = umsMenuService.queryRoleMenusByUserName(userName);
        return CommonResult.success(menus);
    }

    // 更新菜单
    @ResponseBody
    @RequestMapping("/updateMenu")
    public CommonResult updateMenu(UmsMenu menu) {
        int ret = umsMenuService.updateMenu(menu);
        return CommonResult.success(ret);
    }

    // 添加菜单
    @ResponseBody
    @RequestMapping("/addMenu")
    public CommonResult addMenu(UmsMenu menu) {
        int ret = umsMenuService.addMenu(menu);
        return CommonResult.success(ret);
    }

    // 删除菜单
    @ResponseBody
    @RequestMapping("/deleteMenuById")
    public CommonResult deleteMenuById(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "type") String type) {

        int ret = umsMenuService.deleteMenuById(id, type);
        if (ret == 0) {
            return CommonResult.failed("要删除的菜单不存在！");
        } else if (ret == -2) {
            return CommonResult.failed("该目录菜单下还有子目录！");
        } else if (ret == -3) {
            return CommonResult.failed("错误的菜单类型！");
        } else {
            return CommonResult.success("菜单删除成功");
        }
    }
}
